package util;

import element.Bar;
import element.FixedMass;
import element.Mass;
import element.Muscle;
import element.Spring;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import springiesSimulator.Assembly;


/**
 * @author Robert c. Duvall (edited by Mark Govea)
 */
public class Factory {

    /**
     * @param assembly parent simulation
     * @param modelFile blueprint file containing target model
     */
    public void loadModel (Assembly assembly, File modelFile) {
        try {
            Scanner input = new Scanner(modelFile);
            while (input.hasNext()) {
                Scanner line = new Scanner(input.nextLine());
                if (line.hasNext()) {
                    String type = line.next();
                    if ("mass".equals(type)) {
                        assembly.add(massCommand(assembly, line));
                    }
                    else if ("spring".equals(type)) {
                        assembly.add(springCommand(line, assembly));
                    }
                    else if ("muscle".equals(type)) {
                        assembly.add(muscleCommand(line, assembly));
                    }
                }
            }
            input.close();
        }
        catch (FileNotFoundException e) {
            // should not happen because File came from user selection
            e.printStackTrace();
        }
    }

    private Mass massCommand (Assembly assembly, Scanner line) {
        int id = line.nextInt();
        double x = line.nextDouble();
        double y = line.nextDouble();
        double mass = line.nextDouble();
        if (mass < 0) {
            return new FixedMass(assembly, id, x, y, mass);
        }
        else {
            return new Mass(assembly, id, x, y, mass);
        }
    }

    private Spring springCommand (Scanner line, Assembly sim) {
        int[] masses = getSpringMasses(line);
        double[] springValues = getSpringValues(line);
        if (springValues[1] < 0) {
            return new Bar(sim.getMass(masses[0]), sim.getMass(masses[1]),
                    springValues[0], springValues[1]);
        }
        else {
            return new Spring(sim.getMass(masses[0]), sim.getMass(masses[1]),
                    springValues[0], springValues[1]);
        }
    }

    private Spring muscleCommand (Scanner line, Assembly sim) {
        int[] masses = getSpringMasses(line);
        double[] springValues = getSpringValues(line);
        double amp = line.nextDouble();
        return new Muscle(sim.getMass(masses[0]), sim.getMass(masses[1]),
                springValues[0], springValues[1], amp);
    }

    private int[] getSpringMasses(Scanner line) {
        int[] mass = new int[2];
        mass[0] = line.nextInt();
        mass[1] = line.nextInt();
        return mass;
    }

    private double[] getSpringValues(Scanner line) {
        double[] values = new double[2];
        values[0] = line.nextDouble();
        values[1] = line.nextDouble();
        return values;
    }
}
